package cn.znzz.cjm.user.service.impl;


import cn.znzz.cjm.basic.constant.LoginConstant;
import cn.znzz.cjm.basic.exception.BusinessException;
import cn.znzz.cjm.basic.service.impl.BaseServiceImpl;
import cn.znzz.cjm.basic.util.MD5Utils;
import cn.znzz.cjm.basic.util.StrUtils;
import cn.znzz.cjm.org.mapper.EmployeeMapper;
import cn.znzz.cjm.user.domain.Logininfo;
import cn.znzz.cjm.user.domain.User;
import cn.znzz.cjm.user.dto.UserDto;
import cn.znzz.cjm.user.mapper.LogininfoMapper;
import cn.znzz.cjm.user.mapper.UserMapper;
import cn.znzz.cjm.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 业务层实现
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    //基本的crud实现被抽取
    @Override
    public void register(UserDto dto) {
        //1.空校验
        if(StringUtils.isEmpty(dto.getPhone())
                || StringUtils.isEmpty(dto.getVerifyCode())
                || StringUtils.isEmpty(dto.getPassword())
                || StringUtils.isEmpty(dto.getConfirmPassword())){
            throw new BusinessException("请填写完整");
        }
        //2.密码和确认面膜是否一致
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinessException("两次密码必须一致");
        }
        //3.手机号不能注册
        User userTmp = userMapper.loadByPhone(dto.getPhone());
        if(userTmp !=null){
            throw new BusinessException("用户已经存在");
        }
        //4.验证码是否失效 从redis中拿
        Object codeTmp = redisTemplate.opsForValue().get(LoginConstant.PHONE_REGISTER + dto.getPhone());
        //5.没拿到验证码，验证码过期，重新发送
        if(codeTmp == null){
            throw new BusinessException("验证码失效，请重新获取");
        }
        //6.拿到了验证码，判断是否一致，不用区分大小写
        if(!dto.getVerifyCode().equalsIgnoreCase(codeTmp.toString().split(":")[0])){
            throw new BusinessException("验证码错误");
        }
        //7.保存数据t_logininfo
        User user=UserDto2User(dto);
        Logininfo logininfo=user2Logininfo(user);
        logininfoMapper.save(logininfo);
        //8.保存数据t_user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    //吧user传唤为Logininfo
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//一定要设置
        return logininfo;
    }

    //将dto对象转换为user对象
    private User UserDto2User(UserDto dto) {
        User user=new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //生成颜值
        String salt = StrUtils.getComplexRandomString(32);
        //加密密码
        String md5Password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Password);

        return user;
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}















